//Page Object

import { getTabs, getArticleList } from '../../api/index'
Page({
  data: {
    active: 0,
    channelList:[],
    show:false,
    refresher:false,
    articleList: [],
    channel_id: 0,
    timestamp: +new Date(),
    pre_timestamp: +new Date()
  },

  onChange(event) {
    wx.showToast({
      title: `切换到标签 ${event.detail.title}`,
      icon: 'none',
    });
    this.setData({
      articleList: [],
      channel_id: event.detail.name,
      timestamp: +new Date()
    })
    this.getArticleList(this.data.timestamp)
  },

  onPulling() {
    this.setData({
      articleList: [],
      channel_id: this.data.channel_id,
      timestamp: +new Date()
    })
    this.getArticleList(this.data.timestamp)
    setTimeout(() => {
      this.setData({
        refresher:false
      })
    }, 500);
  },

  onBottom() {
    console.log(123);
    console.log(this.data.pre_timestamp);
    this.getArticleList(this.data.pre_timestamp)
  },

  // 获取标签信息
  async getTabs() {
    const res = await getTabs()
    // console.log(res);
    this.setData({
      channelList:res.data.channels
    })
    this.selectComponent('#tabs').resize();
  },

  // 获取文章列表
  async getArticleList(time) {
    // const res = await getArticleList(this.data.pageQuery)
    const res = await getArticleList({
      channel_id: this.data.channel_id,
      with_top: 1,      
      timestamp: time
    })
    // console.log(res);
    this.setData({
      articleList: [...this.data.articleList, ...res.data.results],
      pre_timestamp: res.data.pre_timestamp
    })
  },

  // 点击跳转文章详情
  goArticleDetail(e) {
    // console.log(e); // e.currentTarget.dataset.artid
    wx.navigateTo({
      url: '/pages/detail/detail?artid=' + e.currentTarget.dataset.artid,
    })
  },

  //弹框打开方法回调
  showPopup() {
    this.setData({ show: true });
    wx.hideTabBar()
  },

  // 弹框关闭方法回调
  onClose() {
    this.setData({ show: false });
    wx.showTabBar()
  },

  //options(Object)
  onLoad: function(options){
    if (wx.getStorageSync('token')) {
      wx.setTabBarItem({
        index: 3,
        text: '我的',
        iconPath: "/images/tabs/我的，我.png",
        selectedIconPath: "/images/tabs/我的，我 (1).png"
      })
    }
    
  },
  onReady: function(){
    
  },
  onShow: function(){
    this.getTabs()
    this.getArticleList(this.data.timestamp)
  },
  onHide: function(){

  },
  onUnload: function(){

  },
  onPullDownRefresh: function(){

  },
  onReachBottom: function(){

  },
  onShareAppMessage: function(){

  },
  onPageScroll: function(){

  },
  //item(index,pagePath,text)
  onTabItemTap:function(item){
    console.log(123);
  }
});